import {
  getImage,
  getPage
} from '../../utils/config';
import api from '../../utils/api';
import {
  http
} from '../../utils/utils';
import wxApi from '../../utils/wxApi';
const app = getApp();
import drawQrcode from '../../utils/weapp.qrcode.esm'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    logged: 0,
    abnor: false,
    getImage: getImage,
    couponInfo: [],
    nav_index: 0,
    qrcodeShow: false,
    cou_options: [{
        id: 0,
        text: '未使用'
      },
      {
        id: 1,
        text: '已使用'
      },
      {
        id: 2,
        text: '已过期'
      }
    ],
    query: null
  },

  handleNavbar(e) {
    let nav_index = e.currentTarget.dataset.index;
    console.log('nav_index', nav_index)
    this.setData({
      nav_index
    })
    this.getCoupon();
  },

  /**
   * 获取优惠券
   */
  getCoupon() {
    let {
      userId,
      couponInfo,
      abnor,
      nav_index
    } = this.data;
    http.post(api.default, {
      data: {
        action: 'getusercoupons',
        userid: userId,
        type: nav_index
      }
    },true).then((res) => {
      couponInfo = res.data.data;
      if (couponInfo.length == 0) {
        abnor = true;
      }else{
        abnor = false;
      }
      this.setData({
        couponInfo,
        abnor
      })
    })
  },

  /**
   * 跳转店铺
   */
  visitStore(e) {
    const {
      couponid
    } = e.currentTarget.dataset || e.target.dataset;
    let {
      canvas
    } = this.data;

    this.setData({
      qrcodeShow: true
    })
    let qrtext = 'https://shop.keju123.com/hexiao/'+couponid;
    console.log('qrrrrrrrrrrrrrrrrrr',qrtext)

    // 调用方法drawQrcode生成二维码
    setTimeout(() => {
        drawQrcode({
          canvas: canvas,
          canvasId: 'myQrcode',
          width: 200,
          padding: 30,
          background: '#ffffff',
          foreground: '#000000',
          text: qrtext,
        })
    }, 100);

    // 获取临时路径（得到之后，想干嘛就干嘛了）
    wx.canvasToTempFilePath({
      canvasId: 'myQrcode',
      canvas: canvas,
      x: 0,
      y: 0,
      width: 200,
      height: 200,
      destWidth: 200,
      destHeight: 200,
      success(res) {
        console.log('二维码临时路径：', res.tempFilePath)
        // 存入系统相册
        // wx.saveImageToPhotosAlbum({
        //   filePath: res.tempFilePath || '',
        //   success: res => {
        //     $toast('保存成功', {
        //       icon: 'success'
        //     });
        //   },
        //   fail: res => {
        //     console.log("fail", res)
        //   }
        // })
      },
      fail(res) {
        console.error(res)
      }
    })

    // if(shopno.length == 0){
    //     wxApi.switchTab(getPage.home);
    // }else{
    //     wxApi.navigateTo(getPage.shop, {
    //         id: shopno
    //     });
    // }
  },
  onClose() {
    this.setData({
      qrcodeShow: false
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

    const query = wx.createSelectorQuery();
    console.log('22222222222222',query)
    query.select('#myQrcode')
      .fields({
        node: true,
        size: true
      })
      .exec((res) => {
        console.log('111111111111111111111',res)
        var canvas = res[0].node;
        this.setData({
          canvas
        })
      })

    //获取登录
    app.getLogin().then((res) => {
      this.setData({
        logged: res.code,
        userId: res.data.user.user_id
      });
      //获取优惠券
      this.getCoupon();
    }).catch((res) => {
      this.setData({
        logged: res,
      })
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})